<template>
  <div class="uplaodBox">
    <PageHeader @back="goback" subTitle="返回"> 批量导入 </PageHeader>
    <a-spin :spinning="uploading">
      <div class="ant-upload-select-text-box" style="padding-top: 24px">
        <a-upload ref="upload" :file-list="fileList" :remove="handleRemove" :before-upload="beforeUpload">
          <img :src="uploadImg" style="width: 139px; height: 106px" />
          <div class="ant-modal-body">
            <div style="font-size: 12px; color: #666">点击添加文件或将文件拖拽到此处</div>
            <div style="font-size: 12px; color: #999">(文件大小不超过5M)</div>
          </div>
        </a-upload>
      </div>
    </a-spin>
    <div class="tipBox">
      <a-row style="color: #252525; font-size: 16px; font-weight: 500"> 温馨提示： </a-row>
      <a-row>
        <a-col :span="12" style="line-height: 25px; color: #999">
          <div style="margin-top: 6px">
            1.首先下载标准模板，填写信息后上传<a-button
              size="small"
              type="link"
              @click="download"
              class="primary_color"
              style="font-size: 12px"
              >下载标准模板</a-button
            >
          </div>
          <div>2.支持WPS、Excel2003以上版本文件</div>
          <div>3.请不要加密模板文件，将导致错误</div>
          <div>4.请不要上传带宏的文件，将导致错误</div>
        </a-col>
      </a-row>
    </div>
    <a-modal v-model="dialogFailVisible" title="未成功名单" width="800px" @ok="handleFailClose">
      <a-table :row-key="(d) => d.id" :columns="columns" :data-source="Faildata" :pagination="false" />
    </a-modal>
  </div>
</template>
<script>
import UploadMixin from "xqjr-plugin-common/UploadOrExport/uploadMixin";
import { getToken } from "xqjr-module-auth";
const mixin = new UploadMixin();
const columns = [
  {
    title: "行号",
    dataIndex: "id",
    key: "id"
  },
  {
    title: "姓名",
    dataIndex: "name",
    key: "name"
  },
  {
    title: "原因",
    dataIndex: "reason",
    key: "reason"
  }
];
export default {
  mixins: [mixin],
  data() {
    return {
      columns,
      uploadImg: require("@/assets/img/uploadImg.png"),
      params: {
        templateName: "companyImportTemplate.xlsx"
      },
      url: `/hr/api/v1/excel/template/download`,
      excelName: "用工单位上传模板.xlsx"
    };
  },

  created() {
    this.headers = {
      token: getToken(),
      "Content-Type": "multipart/form-data"
    };
    this.action = `/hr/api/v1/companies/import?force=true`;
  },
  methods: {
    handleSuccess(res) {
      var that = this;
      if (res.success && res.data !== null) {
        var numAll = res.data.importFailList.length + res.data.importSucList.length;
        var numFail = res.data.importFailList.length;
        const numSuc = res.data.importSucList.length;
        if (numFail === 0) {
          this.$confirm({
            okText: "确定",
            title: `共导入${numAll}条，${numSuc}条成功，${numFail}条未成功`,
            onOk() {
              that.goback();
            },
            onCancel() {
              that.goback();
            }
          });
        } else {
          this.$confirm({
            okText: "确定",
            cancelText: "查看未成功名单",
            title: `共导入${numAll}条，${numSuc}条成功，${numFail}条未成功`,
            onOk() {
              that.goback();
            },
            onCancel() {
              that.Faildata = res.data.importFailList;
              that.dialogFailVisible = true;
            }
          });
        }
      } else if (res.data === null) {
        this.$confirm({
          okText: "确定",
          title: res.msg
        });
      } else {
        this.$notification["error"]({
          message: "error",
          description: res.msg || "上传失败"
        });
        setTimeout(() => {
          this.reset();
        }, 2000);
      }
    },
    handleUploadError() {
      this.$message.warning("没有文件");
    }
  }
};
</script>
<style lang="scss" scoped>
.uplaodBox {
  padding-bottom: 24px;
  .head {
    .anticon-left {
      z-index: 3;
      left: 24px;
      position: absolute;
      color: #595959;
      font-size: 20px;
      margin-top: 21px;
      color: #c0c0c0;
    }
    .left2 {
      z-index: 3;
      left: 45px;
      position: absolute;
      font-size: 14px;
      cursor: pointer;
      color: #c0c0c0;
    }
    font-size: 16px;
    color: #000;
    font-weight: 500;
    text-align: center;
    line-height: 60px;
    height: 60px;
    border-bottom: 1px solid #e8e8e8;
  }
  .exportTitle {
    text-align: center;
    font-size: 16px;
    font-weight: 500;
    color: #252525;
  }
  .left2 {
    z-index: 3;
    left: 50px;
    position: absolute;
    color: #595959;
    font-size: 14px;
  }
  .anticon-left {
    z-index: 3;
    left: 24px;
    position: absolute;
    color: #d8d8d8;
    font-size: 26px;
    margin-top: 17px;
  }
  .tipBox {
    line-height: 18px;
    background: #faf9f9;
    border-radius: 4px;
    font-size: 12px;
    color: #595959;
    margin: 0 24px;
    padding: 12px;
    height: 150px;
  }
  .ant-upload-select-text-box {
    border: 1px dashed #b8b8b8;
    background: #fff;
    margin: 24px;
    text-align: center;
    cursor: pointer;
    border-radius: 4px;
    padding: 12px;
  }
  .ant-upload-select-text-box ::v-deep .ant-upload-list {
    display: none;
  }
  .ant-upload-list-item-name {
    text-align-last: left;
  }

  .anticon-upload {
    font-size: 40px;
  }

  .ant-modal-body {
    line-height: 24px;

    em {
      color: var(--primary);
      font-style: normal;
    }
  }
}
</style>
